home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRNOMY / EPHEM421.ZIP / FLOG.C < prev    next >
C/C++ Source or Header  |  1990-09-13  |  3KB  |  122 lines

  1. /* this is a simple little package to manage the saving and retrieving of
  2.  * field values, which we call field logging or "flogs". a flog consists of a
  3.  * field location, ala rcfpack(), its value as a double and its value as
  4.  * a string (ie, however it was printed). you can reset the list of flogs, add
  5.  * to and remove from the list of registered fields and log a field if it has
  6.  * been registered.
  7.  *
  8.  * this is used by the plotting and searching facilities of ephem to maintain
  9.  * the values of the fields that are being plotted or used in search
  10.  * expressions. it is used by the listing facility to generate listing files.
  11.  *
  12.  * a field can be in use for more than one
  13.  * thing at a time (eg, all the X plot values may the same time field, or
  14.  * searching and plotting might be on at one time using the same field) so
  15.  * we consider the field to be in use as long a usage count is > 0.
  16.  */
  17.  
  18. #include "screen.h"
  19.  
  20. extern char *strcpy(), *strncpy();
  21.  
  22. #define    NFLOGS    32        /* max number of distinct simultaneous logged
  23.                  * fields
  24.                  */
  25.  
  26. typedef struct {
  27.     int fl_usagecnt;    /* number of "users" logging to this field */
  28.     int fl_fld;        /* an rcfpack(r,c,0) */
  29.     double fl_val;        /* stored value as a double */
  30.     char fl_str[16];    /* stored value as a formatted string.
  31.                  * N.B.: never overwrite last char: keep as \0
  32.                  */
  33. } FLog;
  34.  
  35. static FLog flog[NFLOGS];
  36.  
  37. /* add fld to the list. if already there, just increment usage count.
  38.  * return 0 if ok, else -1 if no more room.
  39.  */
  40. flog_add (fld)
  41. int fld;
  42. {
  43.     FLog *flp, *unusedflp = 0;
  44.  
  45.     /* scan for fld already in list, or find an unused one along the way */
  46.     for (flp = &flog[NFLOGS]; --flp >= flog; ) {
  47.         if (flp->fl_usagecnt > 0) {
  48.         if (flp->fl_fld == fld) {
  49.             flp->fl_usagecnt++;
  50.             return (0);
  51.         }
  52.         } else
  53.         unusedflp = flp;
  54.     }
  55.     if (unusedflp) {
  56.         unusedflp->fl_fld = fld;
  57.         unusedflp->fl_usagecnt = 1;
  58.         return (0);
  59.     }
  60.     return (-1);
  61. }
  62.  
  63. /* decrement usage count for flog for fld. if goes to 0 take it out of list.
  64.  * ok if not in list i guess...
  65.  */
  66. flog_delete (fld)
  67. int fld;
  68. {
  69.     FLog *flp;
  70.  
  71.     for (flp = &flog[NFLOGS]; --flp >= flog; )
  72.         if (flp->fl_fld == fld && flp->fl_usagecnt > 0) {
  73.         if (--flp->fl_usagecnt <= 0) {
  74.             flp->fl_usagecnt = 0;
  75.         }
  76.         break;
  77.         }
  78. }
  79.  
  80. /* if plotting, listing or searching is active then
  81.  * if rcfpack(r,c,0) is in the fld list, set its value to val.
  82.  * return 0 if ok, else -1 if not in list.
  83.  */
  84. flog_log (r, c, val, str)
  85. int r, c;
  86. double val;
  87. char *str;
  88. {
  89.     if (plot_ison() || listing_ison() || srch_ison()) {
  90.         FLog *flp;
  91.         int fld = rcfpack (r, c, 0);
  92.         for (flp = &flog[NFLOGS]; --flp >= flog; )
  93.         if (flp->fl_fld == fld && flp->fl_usagecnt > 0) {
  94.             flp->fl_val = val;
  95.             (void) strncpy (flp->fl_str, str, sizeof(flp->fl_str)-1);
  96.             return(0);
  97.         }
  98.         return (-1);
  99.     } else
  100.         return (0);
  101. }
  102.  
  103. /* search for fld in list. if find it, return its value and str, if str.
  104.  * return 0 if found it, else -1 if not in list.
  105.  */
  106. flog_get (fld, vp, str)
  107. int fld;
  108. double *vp;
  109. char *str;
  110. {
  111.     FLog *flp;
  112.  
  113.     for (flp = &flog[NFLOGS]; --flp >= flog; )
  114.         if (flp->fl_fld == fld && flp->fl_usagecnt > 0) {
  115.         *vp = flp->fl_val;
  116.         if (str) 
  117.             (void) strcpy (str, flp->fl_str);
  118.         return (0);
  119.         }
  120.     return (-1);
  121. }
  122.